package com.SouthernWall_404.age_tool.Client.Gui.Screen;

import com.SouthernWall_404.age_tool.AgeTool;
import com.SouthernWall_404.age_tool.common.API.NBT.MusicBlockInfo;
import com.SouthernWall_404.age_tool.common.world.Entity.MusicBlockEntity;
import com.SouthernWall_404.age_tool.common.world.Inventory.MusicBlockMenu;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.client.gui.widget.ForgeSlider;

import static com.mojang.blaze3d.platform.InputConstants.KEY_E;

public class MusicBlockScreen extends AbstractContainerScreen<MusicBlockMenu> {

    private BlockEntity blockEntity;
    private Inventory inventory;
    private static final String GUI="gui."+AgeTool.MODID+".";
    //播放距离
    //音量
    //是否衰减
    private EditBox editBox;//音乐所在(输入)
    private ForgeSlider radiusSlider;
    private ForgeSlider volumnSlider;
    float volumn=0;
    int radius=0;



    //private Button advance_button= Button.builder(Component.translatable(GUI+"music_block.button.advanced_setting"),b->{}).build();
    private Button button=Button.builder(Component.translatable(GUI+"music_block.button.confirm"),b->save()).build();
    private ResourceLocation musicLocation=new ResourceLocation(AgeTool.MODID,"test");//音乐路径

    public MusicBlockScreen(MusicBlockMenu menu, Inventory inv, Component component) {
        super(menu, inv, component);

        this.inventory=inv;
        this.blockEntity=menu.getBlockEntity();//获取到blockEntity

//        inv.player.sendSystemMessage(Component.translatable(""+this.blockEntity+","+inv.player.level()));

    }

    /*
    在这里写编辑器
    -记得写同步
     */
    @Override
    protected void init() {

        super.init();

        renderWeight();
    }

    @Override
    protected void containerTick() {
        super.containerTick();
        this.editBox.tick();
    }

    private void renderWeight()
    {
        renderMusicEditBox();

        renderRadiusBox();
        renderVolumnBox();

        addComfirmButton();

        //addAdvancedButton();





    }
    private void renderVolumnBox()
    {
        int l=148;
        int h=20;
        int n1 = (this.width - l) / 2;
        int n2 = (this.height - h) / 2;
        this.volumnSlider = new ForgeSlider(

                n1,
                n2+h/2+3,
                l,
                h,
                Component.translatable(GUI+"music_block.button.volumn"),
                Component.empty(),
                0,
                1.0,
                this.volumn,
                0.05,
                1,
                true);
        MusicBlockEntity musicBlockEntity=(MusicBlockEntity) this.blockEntity;
        CompoundTag modPack=musicBlockEntity.getOrCreateModPack();
        if(modPack!=null) {
            MusicBlockInfo musicBlockInfo = musicBlockEntity.getMusicBlockSetting();
            this.musicLocation= musicBlockInfo.musicLocation;
            this.volumnSlider.setValue(musicBlockInfo.volumn);
        }


        this.addRenderableWidget(this.volumnSlider);
    }
    private void renderRadiusBox()
    {
        int l=148;
        int h=20;
        int n1 = (this.width - l) / 2;
        int n2 = (this.height - h) / 2;
        this.radiusSlider = new ForgeSlider(
                n1,
                n2-h/2-3,
                l,
                h,
                Component.translatable(GUI+"music_block.button.radius"),
                Component.empty(),
                0,
                128,this.radius,1.0,1,true);

        MusicBlockEntity musicBlockEntity=(MusicBlockEntity) this.blockEntity;
        CompoundTag modPack=musicBlockEntity.getOrCreateModPack();
        if(modPack!=null) {
            MusicBlockInfo musicBlockInfo = musicBlockEntity.getMusicBlockSetting();
            this.musicLocation= musicBlockInfo.musicLocation;
            this.radiusSlider.setValue(musicBlockInfo.radius);
        }




        this.addRenderableWidget(this.radiusSlider);
    }
    private void addComfirmButton()
    {
        int l=148;
        int h=20;
        int n1 = (this.width - l) / 2;
        int n2 = (this.height - h) / 2;

        button.setHeight(h);
        button.setWidth(l);
        button.setPosition(n1,n2+h/2*3+9);

        addRenderableWidget(button);
    }
    /*
    储存函数，所有的数据在此输入汇总，发送到远端
     */
    private void save()
    {

        MusicBlockEntity musicBlockEntity=(MusicBlockEntity)this.blockEntity;

        this.radius=(int) this.radiusSlider.getValue();
        this.volumn=(float) this.volumnSlider.getValue();
        musicBlockEntity.setMusicBlockSetting(musicLocation,this.radius,this.volumn);

        //MusicBlockHandler.setMusicBlockSetting(this.musicLocation,this.blockEntity);

        Player player= inventory.player;
        //player.sendSystemMessage(Component.literal(""+MusicBlockHandler.getMusicBlockSetting(this.blockEntity).musicLocation+","+player.level()));
        this.onClose();

    }
    /*
    渲染EditBox
     */
    private void renderMusicEditBox()
    {
        int l=148;
        int h=20;
        int n1 = (this.width - l) / 2;
        int n2 = (this.height - h) / 2;
        this.editBox = new EditBox(
                this.font,
                n1,
                n2-h/2*3-9,
                l,
                h,
                Component.translatable("container.repair"));
//        this.name.set
//        this.name.setEditable(true);
//        this.name.setBordered(true);

        MusicBlockEntity musicBlockEntity=(MusicBlockEntity) this.blockEntity;
        CompoundTag modPack=musicBlockEntity.getOrCreateModPack();
        if(modPack!=null) {
            MusicBlockInfo musicBlockInfo = musicBlockEntity.getMusicBlockSetting();
            this.musicLocation= musicBlockInfo.musicLocation;
            this.editBox.setValue(musicBlockInfo.musicLocation.toString());
        }
        this.editBox.setResponder(this::onStringChanged);

        this.editBox.setCanLoseFocus(false);
        this.editBox.setTextColor(-1);
        this.editBox.setTextColorUneditable(-1);
        this.editBox.setBordered(true);
        this.editBox.setMaxLength(50);
//        this.editBox.setValue("");
        this.editBox.setResponder(this::onStringChanged);
        this.addWidget(this.editBox);
        this.setInitialFocus(this.editBox);
        this.editBox.setEditable(true);
    }


    private void onStringChanged(String string)
    {
        if(ResourceLocation.isValidResourceLocation(string))
        {
            musicLocation=new ResourceLocation(string);
        }
        else
        {
            Minecraft.getInstance().player.sendSystemMessage(Component.translatable(GUI+"music_block.error.resource_location").withStyle(style ->style.withColor(ChatFormatting.RED)));
            this.musicLocation=new ResourceLocation("age_tool:null");
            this.save();
        }
//        Minecraft.getInstance().player.sendSystemMessage(Component.literal(""+musicLocation));
//        this.e.setValue(string);
    }

    @Override
    protected void renderLabels(GuiGraphics p_281635_, int p_282681_, int p_283686_) {
        //super.renderLabels(p_281635_, p_282681_, p_283686_);
    }

    @Override
    protected void renderBg(GuiGraphics guiGraphics, float f, int n, int n2) {
        int n3 = this.leftPos;
        int n4 = (this.height - this.imageHeight) / 2;
        this.editBox.render(guiGraphics,n,n2,1f);
//         renderDirtBackground(guiGraphics);
//        renderBackground(guiGraphics);
    }



    @Override
    public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float delta) {
        renderBackground(guiGraphics);

        super.render(guiGraphics, mouseX, mouseY, delta);


    }
    @Override
    public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
        if (keyCode == KEY_E) {
//            Minecraft.getInstance().player.displayClientMessage(
//                    Component.literal("你按下了 E 键，但屏幕没有关闭！"),
//                    false
//            );
            return true;
        }
        return super.keyPressed(keyCode,scanCode,modifiers);
    }
}
